"""__author__=Hanlin"""
import redis

client = redis.Redis(host='47.96.83.83', port=6379, password='1qaz2wsx')
print(client)
print(client.ping())
if client.ping():
    # bytes ---> decode('utf-8') ---> str
    # str ---> encode('utf-8') ---> bytes
    print(client.ttl('username'))
    # print(client.get('username').decode('utf-8'))
    print(client.ttl('password'))
    print(client.get('password'))
    # client.set('username', '张瀚霖', ex=120)
    # client.set('password', '12345')
    client.rpush('list1', 1,2,3,4,5,6)
    list1 = client.lrange('list1', 0, -1)
    print(list1)
    print(list(map(int, list1)))
    item = client.hgetall('stu1')
    print(list(map(bytes.decode, item)))
    client.zadd('zset1',
                {
                    'yuting':100,
                    'luohao':90,
                    'wang':10
                }
                )


else:
    print('连接失败!!!')

"""
序列化:把一个对象变成字符串（str）或者字节串（bytes）
反序列化:把字符串或者字节串还原成对象
json模块 - dumps/loads - 字符串
pickle - dumps/loads - 字节串
"""
import pickle
class Student:
    def __init__(self,stuid,name,sex,addr):
        self.stuid = stuid
        self.name = name
        self.sex = sex
        self.addr = addr
    
    def __repr__(self):
        return '%s: %s'% (self.name, '男'if self.sex else '女')

    # @staticmethod
    # def add_student(self):

# class StudentJasonEncoder(json.JSONEncoder):
#
#     def default(self, o):
#         return o.__dict__
        # return {
        #     'id': o.stuid,
        #     'name': o.name,
        #     'sex': o.sex,
        #     'addr': o.addr
        #
        # }

stu1 = Student(1001, '骆昊', True, '四川成都')
stu2 = Student(1002, '王大锤', True, '四川绵阳')
students = [stu1,stu2]



print(pickle.dumps(students))
# client = redis.Redis(host='47,96,83,83', port=6379, password='1qaz2wsx')

client.set('students', pickle.dumps(students))
print(pickle.loads(client.get('students')))
client.get('students')
# print(stu1.name)

# client.hmset('student_info', {'stuid': stu1.stuid, 'stuname': stu1.name, 'stugender': stu1.sex})